﻿<?php
$context = $_GET['page'];

if($context == 'account_edit' && isset($_POST['connexion'])){
	include "pages/controller/account.php";
} else {
	$hasSecondAdress = true;
	if($context == 'account_edit'){
		$conn = unserialize($_SESSION['connexion']);
		$q = new ClientQuery();
		$client = $q->findOneByEmail($conn->login);
				
		$addr = AdresseQuery::create()->findPk($client->getAdresse());
		
		
		$title = $addr->getTitre() == 0 ? "Mr" : "Mme";
		$name = $addr->getNom();
		$firstname = $addr->getPrenom();
		$address = $addr->getAdresse();
		$postalCode = $addr->getPostal();
		$city = $addr->getVille();
		$phone = $addr->getTel();
		$mail = $conn->login;
		
		if($client->getFacturation() != NULL){
			$addrDelivery = AdresseQuery::create()->findPk($client->getFacturation());
			$titleDelivery = $addrDelivery->getTitre() == 0 ? "Mr" : "Mme";
			$nameDelivery = $addrDelivery->getNom();
			$firstnameDelivery = $addrDelivery->getPrenom();
			$addressDelivery = $addrDelivery->getAdresse();
			$postalCodeDelivery = $addrDelivery->getPostal();
			$cityDelivery = $addrDelivery->getVille();
			$phoneDelivery = $addrDelivery->getTel();
		} else {
			$hasSecondAdress = false;
			$titleDelivery = "Mr";
			$nameDelivery = "";
			$firstnameDelivery = "";
			$addressDelivery = "";
			$postalCodeDelivery = "";
			$cityDelivery = "";
			$phoneDelivery = "";
		}
	
	} else {

		$title = isset($_POST['title']) ? $_POST['title'] : "Mr";
		$name = isset($_POST['name']) ? $_POST['name'] : "";
		$firstname = isset($_POST['firstname']) ? $_POST['firstname'] : "";
		$address = isset($_POST['address']) ? $_POST['address'] : "";
		$postalCode = isset($_POST['postalCode']) ? $_POST['postalCode'] : "";
		$city = isset($_POST['city']) ? $_POST['city'] : "";
		$phone = isset($_POST['phone']) ? $_POST['phone'] : "";
		$mail = isset($_POST['mail']) ? $_POST['mail'] : "";
		
		$titleDelivery = isset($_POST['titleDelivery']) ? $_POST['titleDelivery'] : "Mr";
		$nameDelivery = isset($_POST['nameDelivery']) ? $_POST['nameDelivery'] : "";
		$firstnameDelivery = isset($_POST['firstnameDelivery']) ? $_POST['firstnameDelivery'] : "";
		$addressDelivery = isset($_POST['addressDelivery']) ? $_POST['addressDelivery'] : "";
		$postalCodeDelivery = isset($_POST['postalCodeDelivery']) ? $_POST['postalCodeDelivery'] : "";
		$cityDelivery = isset($_POST['cityDelivery']) ? $_POST['cityDelivery'] : "";
		$phoneDelivery = isset($_POST['phoneDelivery']) ? $_POST['phoneDelivery'] : "";
	
	}
	
	$smarty->assign("context", $context);
	$smarty->assign("customer", array("title" => $title, "name" => $name, "firstname" => $firstname, "address" => $address, "postalCode" => $postalCode, "city" => $city, "phone" => $phone, "mail" => $mail));
	$smarty->assign("hasSecondAdress", $hasSecondAdress);
	$smarty->assign("customerBis", array("titleDelivery" => $titleDelivery, "nameDelivery" => $nameDelivery, "firstnameDelivery" => $firstnameDelivery, "addressDelivery" => $addressDelivery, "postalCodeDelivery" => $postalCodeDelivery, "cityDelivery" => $cityDelivery, "phoneDelivery" => $phoneDelivery));
	$smarty->display('account_edit.tpl');
}



